<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.view
 * @subpackage  helper
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

/**
 * Pomocnik widoku, obraz medium
 *
 * @category    source
 * @package     pl.nweb.editor.view
 * @subpackage  helper
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Zend_View_Helper_MediaImage extends Zend_View_Helper_Abstract
{
    /**
     * Katalog
     *
     * @var string
     */
    private $_dir = null;

    /**
     * Hash
     *
     * @var string
     */
    private $_hash = null;

    /**
     * Rozszerzenie pliku
     *
     * @var string
     */
    private $_extension = null;

    /**
     * Zwraca instancję obiektu
     *
     * @param  integer $id id pliku
     * @param  string $language język
     * @param  string $extension rozszerzenie pliku
     * @return Zend_View_Helper_MediaImage
     */
    public function mediaImage ($id, $language, $extension)
    {
        $this->_hash      = sha1($id);
        $this->_extension = '.' . ltrim($extension, '.');
        $this->_dir       = $this->view->baseUrl('media/data/' . $language . '/' . substr($this->_hash, 0, 3));

        return $this;
    }

    /**
     * Zwraca adres url do obrazka
     *
     * @param null|integer $width szerokość
     * @param null|integer $height wysokość
     * @return string
     */
    public function url ($width = null, $height = null)
    {
        if(null !== $width && null !== $height) {
            return implode('/', array($this->_dir, $this->_hash, ($width . '-' . $height . $this->_extension)));
        } else {
            return implode('/', array($this->_dir, ($this->_hash . $this->_extension)));
        }
    }
}
